package com.prs.crm.action.crm;

import java.io.File;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.annotation.Clew;
import com.prs.crm.context.AppContext;
import com.prs.crm.domain.crm.Feedback;
import com.prs.crm.domain.crm.FeedbackReply;
import com.prs.crm.service.crm.FeedbackReplyService;
import com.prs.crm.service.crm.FeedbackService;

public class FeedbackReplyAction extends BaseAction {
	private Feedback feedback;
	private FeedbackService feedbackService;
	private FeedbackReply feedbackReply;
	private FeedbackReplyService feedbackReplyService;
	private PaginationSupport pagination;

	private File upload;
	private String uploadFileName;

	@Action("listWaitFeedbackReply")
	public String listWaitFeedbackReply() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Feedback.class);
		criteria.add(Restrictions.eq("isReply", "是"));
		criteria.add(Restrictions.eq("replyer", AppContext.getAccountContext()
				.currentUser()));
		criteria.addOrder(Order.desc("recordTime"));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getFeedbackReplyService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("listFeedbackReply")
	public String listFeedbackReply() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				FeedbackReply.class);
		criteria.addOrder(Order.desc("recordTime"));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getFeedbackReplyService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("editFeedbackReply")
	public String editFeedbackReply() {
		return SUCCESS;
	}

	@Clew(results = { "success" })
	@Action(value = "submitFeedbackReply", results = {
			@Result(name = "success", type = "redirect", location = "listWaitFeedbackReply"),
			@Result(name = "error", type = "chain", location = "editFeedbackReply") })
	public String submitFeedbackReply() {
		if (feedbackReply.getLinkman() == null) {
			addActionError("联系人不能为空");
			return ERROR;
		}
		if (feedbackReply.getTitle() == null
				|| "".equals(feedbackReply.getTitle().trim())) {
			addActionError("主题不能为空");
			return ERROR;
		}
		if (feedbackReply.getResult() == null
				|| "".equals(feedbackReply.getResult().trim())) {
			addActionError("答复结果不能为空");
			return ERROR;
		}
		if (feedbackReply.getWay() == null
				|| feedbackReply.getWay().trim().equals("")) {
			addActionError("联系方式不能为空");
			return ERROR;
		}
		feedbackReply.setFeedback(feedback);
		this.getFeedbackReplyService().submitFeedbackReply(feedbackReply,
				upload, uploadFileName);
		this.setPrompt("提交客户反馈答复成功");
		return SUCCESS;
	}

	@Action("viewFeedbackReply")
	public String viewFeedbackReply() {
		return SUCCESS;
	}

	public FeedbackReply getFeedbackReply() {
		return feedbackReply;
	}

	public void setFeedbackReply(FeedbackReply feedbackReply) {
		this.feedbackReply = feedbackReply;
	}

	public FeedbackReplyService getFeedbackReplyService() {
		return feedbackReplyService;
	}

	public void setFeedbackReplyService(
			FeedbackReplyService feedbackReplyService) {
		this.feedbackReplyService = feedbackReplyService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public Feedback getFeedback() {
		return feedback;
	}

	public void setFeedback(Feedback feedback) {
		this.feedback = feedback;
	}

	public FeedbackService getFeedbackService() {
		return feedbackService;
	}

	public void setFeedbackService(FeedbackService feedbackService) {
		this.feedbackService = feedbackService;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
}
